package com.evil.common.application.enums.form;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 规则类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum RuleTypeEnum {

    /**
     * 规则类型(0:自增值, 1: 提交日期, 2:固定字符, 3:表单内字段)
     */
    INCREMENT(0, "自增值"),
    SUBMIT_DATE(1, "提交日期"),
    FIXED(2, "固定字符"),
    FORM_DATA(3, "表单内字段"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<RuleTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static RuleTypeEnum isExistById(Integer id) {
        return RuleTypeEnum.findById(id).orElseThrow(RuleTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "规则类型参数错误");
    }
}
